package com.cyzy.homework.repository;

import com.cyzy.homework.entity.HomeworkFile;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * 作业文件Repository
 */
@Repository
public interface HomeworkFileRepository extends JpaRepository<HomeworkFile, Long> {
    
    /**
     * 根据作业提交ID查询文件列表，按上传时间倒序
     */
    List<HomeworkFile> findByHomeworkSubmitIdOrderByUploadTimeDesc(Long homeworkSubmitId);
    
    /**
     * 根据上传者ID查询文件列表，按上传时间倒序
     */
    List<HomeworkFile> findByUploaderIdOrderByUploadTimeDesc(String uploaderId);
    
    /**
     * 根据文件类型查询文件列表
     */
    List<HomeworkFile> findByFileTypeOrderByUploadTimeDesc(String fileType);
    
    /**
     * 根据上传时间范围查询文件列表
     */
    @Query("SELECT hf FROM HomeworkFile hf WHERE hf.uploadTime BETWEEN :startTime AND :endTime ORDER BY hf.uploadTime DESC")
    List<HomeworkFile> findByUploadTimeBetween(@Param("startTime") LocalDateTime startTime, 
                                              @Param("endTime") LocalDateTime endTime);
    
    /**
     * 根据上传者ID和文件类型查询文件列表
     */
    List<HomeworkFile> findByUploaderIdAndFileTypeOrderByUploadTimeDesc(String uploaderId, String fileType);
    
    /**
     * 根据作业提交ID和文件类型查询文件列表
     */
    List<HomeworkFile> findByHomeworkSubmitIdAndFileTypeOrderByUploadTimeDesc(Long homeworkSubmitId, String fileType);
    
    /**
     * 统计作业提交的文件数量
     */
    long countByHomeworkSubmitId(Long homeworkSubmitId);
    
    /**
     * 统计上传者的文件数量
     */
    long countByUploaderId(String uploaderId);
    
    /**
     * 统计指定文件类型的文件数量
     */
    long countByFileType(String fileType);
    
    /**
     * 根据原始文件名查询文件
     */
    Optional<HomeworkFile> findByOriginalName(String originalName);
    
    /**
     * 根据存储文件名查询文件
     */
    Optional<HomeworkFile> findByStoredName(String storedName);
    
    /**
     * 根据文件路径查询文件
     */
    Optional<HomeworkFile> findByFilePath(String filePath);
    
    /**
     * 查询指定大小范围的文件
     */
    @Query("SELECT hf FROM HomeworkFile hf WHERE hf.fileSize BETWEEN :minSize AND :maxSize ORDER BY hf.uploadTime DESC")
    List<HomeworkFile> findByFileSizeBetween(@Param("minSize") Long minSize, @Param("maxSize") Long maxSize);
    
    /**
     * 查询超过指定大小的文件
     */
    @Query("SELECT hf FROM HomeworkFile hf WHERE hf.fileSize > :size ORDER BY hf.uploadTime DESC")
    List<HomeworkFile> findByFileSizeGreaterThan(@Param("size") Long size);
    
    /**
     * 根据作业提交ID列表查询文件
     */
    @Query("SELECT hf FROM HomeworkFile hf WHERE hf.homeworkSubmitId IN :submitIds ORDER BY hf.uploadTime DESC")
    List<HomeworkFile> findByHomeworkSubmitIdIn(@Param("submitIds") List<Long> submitIds);
    
    /**
     * 删除指定作业提交的所有文件
     */
    void deleteByHomeworkSubmitId(Long homeworkSubmitId);
    
    /**
     * 删除指定上传者的所有文件
     */
    void deleteByUploaderId(String uploaderId);
}

